namespace CommontLibrary
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    /// <summary>
    /// Summary description for BindData
    /// </summary>
    public class BindData
    {
        public BindData()
        {
            //
            // TODO: Add constructor logic here
            //
        }

#region "Bind Control"
        //Bind du lieu vao Label
        public static void bindLabel(Label lblTemp, string strToolTip, string strText)
        {
            lblTemp.Text = strText.Trim();
            lblTemp.ToolTip = strToolTip.Trim();
        }        
        //Bind du lieu vao TextBox
        public static void bindTextBox(TextBox txtTemp, string strToolTip, string strText)
        {
            txtTemp.Text = strText.Trim();
            txtTemp.ToolTip = strToolTip.Trim();
        }
        //Bind du lieu vao HyperLink
        public static void bindHyperLink(HyperLink hplTemp, string strNavigateUrl, string strText, string strImageURL)
        {
            if (!strText.Trim().Equals(""))
            {
                hplTemp.Text = strText.Trim();
            }
            if (!strNavigateUrl.Trim().Equals(""))
            {
                hplTemp.NavigateUrl = strNavigateUrl.Trim();
            }
            if (!strNavigateUrl.Trim().Equals(""))
            {
                hplTemp.ImageUrl = strImageURL.Trim();
            }
        }
        //Bind du lieu vao CheckBox
        public static void bindCheckBox(CheckBox cbTemp, bool boolTemp, string strText)
        {
            if (!strText.Trim().Equals(""))
            {
                cbTemp.Text = strText.Trim();
            }
            cbTemp.Checked = boolTemp;
        }
        //Bind du lieu vao Image
        public void bindImage(Image imageTemp, string strToolTip, string strImageUrl)
        {
            if (!strToolTip.Trim().Equals(""))
            {
                imageTemp.ToolTip = strToolTip.Trim();
            }
            imageTemp.ImageUrl = strImageUrl.Trim();
        }
        //Bind du lieu vao RadioButton
        public static void bindRadioButton(RadioButton rbTemp, bool boolTemp, string strText)
        {
            if (!strText.Trim().Equals(""))
            {
                rbTemp.Text = strText.Trim();
            }
            rbTemp.Checked = boolTemp;
        }
        //Bind du lieu vao  DropDownList theo DataRow[]
        public static void bindDropDownList(DropDownList ddlTemp, DataRow[] drTemps, string strValue, string strText)
        {
            ddlTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                ddlTemp.Items.Add(liTemp);               
            }
        }
        //Bind du lieu vao  DropDownList theo DataRowCollection
        public static void bindDropDownList(DropDownList ddlTemp,DataRowCollection drTemps, string strValue, string strText)
        {
            ddlTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                ddlTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  DropDownList theo DataTable
        public static void bindDropDownList(DropDownList ddlTemp, DataTable dtTemp, string strValue, string strText)
        {
            ddlTemp.Items.Clear();
            ddlTemp.DataSource = dtTemp;
            ddlTemp.DataTextField = strText.Trim();
            ddlTemp.DataValueField = strValue.Trim();
            ddlTemp.DataBind();
        }
        //Bind du lieu vao  DropDownList theo DataSet
        public static void bindDropDownList(DropDownList ddlTemp, DataSet dsTemp, string strValue, string strText)
        {
            ddlTemp.Items.Clear();
            ddlTemp.DataSource = dsTemp;
            ddlTemp.DataTextField = strText.Trim();
            ddlTemp.DataValueField = strValue.Trim();
            ddlTemp.DataBind();
        }
        public static void bindDropDownNotClearItems(DropDownList ddlTemp, DataSet dsTemp, string strValue, string strText)
        {
            //ddlTemp.Items.Clear();
            ddlTemp.DataSource = dsTemp;
            ddlTemp.DataTextField = strText.Trim();
            ddlTemp.DataValueField = strValue.Trim();
            ddlTemp.DataBind();
        }
        //Bind du lieu vao  ListBox theo DataRow[]
        public static void bindListBox(ListBox lxTemp, DataRow[] drTemps, string strValue, string strText)
        {
            lxTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                lxTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  ListBox theo DataRowCollection
        public static void bindListBox(ListBox lxTemp, DataRowCollection drTemps, string strValue, string strText)
        {
            lxTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                lxTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  ListBox theo DataTable
        public static void bindListBox(ListBox lxTemp, DataTable dtTemp, string strValue, string strText)
        {
            lxTemp.Items.Clear();
            lxTemp.DataSource = dtTemp;
            lxTemp.DataTextField = strText.Trim();
            lxTemp.DataValueField = strValue.Trim();
            lxTemp.DataBind();
        }
        //Bind du lieu vao  ListBox theo DataSet
        public static void bindListBox(ListBox lxTemp, DataSet dsTemp, string strValue, string strText)
        {
            lxTemp.Items.Clear();
            lxTemp.DataSource = dsTemp;
            lxTemp.DataTextField = strText.Trim();
            lxTemp.DataValueField = strValue.Trim();
            lxTemp.DataBind();
        }
        //Bind du lieu vao  CheckBoxList theo DataRow[]
        public static void bindCheckBoxList(CheckBoxList cblTemp, DataRow[] drTemps, string strValue, string strText)
        {
            cblTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                cblTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  CheckBoxList theo DataRowCollection
        public static void bindCheckBoxList(CheckBoxList cblTemp,DataRowCollection drTemps, string strValue, string strText)
        {
            cblTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                cblTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  CheckBoxList theo DataTable
        public static void bindCheckBoxList(CheckBoxList cblTemp, DataTable dtTemp, string strValue, string strText)
        {
            cblTemp.Items.Clear();
            cblTemp.DataSource = dtTemp;
            cblTemp.DataTextField = strText.Trim();
            cblTemp.DataValueField = strValue.Trim();
            cblTemp.DataBind();
        }
        //Bind du lieu vao  CheckBoxList theo DataSet
        public static void bindCheckBoxList(CheckBoxList cblTemp, DataSet dsTemp, string strValue, string strText)
        {
            cblTemp.Items.Clear();
            cblTemp.DataSource = dsTemp;
            cblTemp.DataTextField = strText.Trim();
            cblTemp.DataValueField = strValue.Trim();
            cblTemp.DataBind();
        }
        //Bind du lieu vao  RadioButtonList theo DataRow[]
        public static void bindRadioButtonList(RadioButtonList rblTemp, DataRow[] drTemps, string strValue, string strText)
        {
            rblTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                rblTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  RadioButtonList theo DataRowCollection
        public static void bindRadioButtonList(RadioButtonList rblTemp, DataRowCollection drTemps, string strValue, string strText)
        {
            rblTemp.Items.Clear();
            foreach (DataRow dr in drTemps)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = dr[strText].ToString();
                liTemp.Value = dr[strValue].ToString();
                rblTemp.Items.Add(liTemp);
            }
        }
        //Bind du lieu vao  RadioButtonList theo DataTable
        public void bindRadioButtonList(RadioButtonList rblTemp, DataTable dtTemp, string strValue, string strText)
        {
            rblTemp.Items.Clear();
            rblTemp.DataSource = dtTemp;
            rblTemp.DataTextField = strText.Trim();
            rblTemp.DataValueField = strValue.Trim();
            rblTemp.DataBind();
        }
        //Bind du lieu vao  RadioButtonList theo DataSet
        public void bindRadioButtonList(RadioButtonList rblTemp, DataSet dsTemp, string strValue, string strText)
        {
            rblTemp.Items.Clear();
            rblTemp.DataSource = dsTemp;
            rblTemp.DataTextField = strText.Trim();
            rblTemp.DataValueField = strValue.Trim();
            rblTemp.DataBind();
        }
        //Bind du lieu vao  GridView theo DataTale
        public static void bindGridView(GridView gvTemp, DataTable dtTemp)
        {
            gvTemp.DataSource = dtTemp;
            gvTemp.DataBind();
        }
        //Bind du lieu vao  GridView theo DataSet
        public static void bindGridView(GridView gvTemp, DataSet dsTemp)
        {
            gvTemp.DataSource = dsTemp;
            gvTemp.DataBind();
        }
        //Bind du lieu vao  DataList theo DataTable
        public static void bindDataList(DataList dlTemp, DataTable dtTemp)
        {
            dlTemp.DataSource = dtTemp;
            dlTemp.DataBind();
        }
        //Bind du lieu vao  DataList theo DataSet
        public static void bindDataList(DataList dlTemp, DataSet dsTemp)
        {
            dlTemp.DataSource = dsTemp;
            dlTemp.DataBind();
        }
        //Bind du lieu vao  DetailsView theo DataTable
        public static void bindDetailsView(DetailsView dtvTemp, DataTable dtTemp)
        {
            dtvTemp.DataSource = dtTemp;
            dtvTemp.DataBind();
        }
        //Bind du lieu vao  DetailsView theo DataSet
        public static void bindDetailsView(DetailsView dtvTemp, DataSet dsTemp)
        {
            dtvTemp.DataSource = dsTemp;
            dtvTemp.DataBind();
        }
        //Bind du lieu vao FormView theo DataTable
        public static void bindFormView(FormView fvTemp, DataTable dtTemp)
        {
            fvTemp.DataSource = dtTemp;
            fvTemp.DataBind();
        }
        //Bind du lieu vao FormView theo DataSet
        public static void bindFormView(FormView fvTemp, DataSet dsTemp)
        {
            fvTemp.DataSource = dsTemp;
            fvTemp.DataBind();
        }
        //Reset du lieu cua CheckBoxList 
        public static void resetCheckBoxList(CheckBoxList cblTemp, bool boolCheck)
        {
            for (int i = 0; i < cblTemp.Items.Count; i++)
            {
                cblTemp.Items[i].Selected = boolCheck;
            }
        }
        //Reset du lieu cua RadioButtonList
        public static void resetRadioButtonList(RadioButtonList rblTemp, bool boolCheck)
        {
            for (int i = 0; i < rblTemp.Items.Count;)
            {
                rblTemp.Items[i].Selected = boolCheck;
                break;
            }
        }
#endregion

#region "Select Control"
        //Select du lieu cua DropDownList
        public static void selectDropDowList(DropDownList ddlTemp, string strValue)
        {
            for (int i = 0; i < ddlTemp.Items.Count; i++)
            {
                ddlTemp.Items[i].Selected = false;
            }
            for (int i = 0; i < ddlTemp.Items.Count; i++)
            {
                if (ddlTemp.Items[i].Value.Trim() == strValue.Trim())
                {
                    ddlTemp.Items[i].Selected = true;
                    break;
                }
            }
        }
        //Lay 1 cot tu Talbe tra ra kieu mang string
        public static string[] stringRows(DataTable dtTemp, string strKey)
        {
            string[] strTemp = new string[dtTemp.Rows.Count];
            for (int i = 0; i < dtTemp.Rows.Count; i++)
            {
                strTemp[i] = dtTemp.Rows[i][strKey].ToString().Trim();
            }
            return strTemp;
        }
        //Select du lieu cua ListBox
        public static void selectListBox(ListBox lbTemp, string[] strValues)
        {
            for (int j = 0; j < strValues.Length; j++)
            {
                for (int i = 0; i < lbTemp.Items.Count; i++)
                {
                    if (lbTemp.Items[i].Value.Trim() == strValues[j].Trim())
                    {
                        lbTemp.Items[i].Selected = true;
                        break;
                    }
                }
            }
        }
        //Select du lieu cua CheckBoxList
        public static void selectCheckBoxList(CheckBoxList cblTemp, string[] strValues)
        {
            for (int j = 0; j < strValues.Length; j++)
            {
                for (int i = 0; i < cblTemp.Items.Count; i++)
                {
                    if (cblTemp.Items[i].Value.Trim() == strValues[j].Trim())
                    {
                        cblTemp.Items[i].Selected = true;
                    }
                }
            }
        }
        //Select du lieu cua CheckBoxList theo DataSet
        public static void selectCheckBoxList(CheckBoxList cblTemp, DataSet dsTemp, string strID)
        {
            for (int j = 0; j < dsTemp.Tables[0].Rows.Count; j++)
            {
                int intID = Convert.ToInt32(dsTemp.Tables[0].Rows[j][strID]);
                for (int i = 0; i < cblTemp.Items.Count; i++)
                {
                    int intSelect = Convert.ToInt32(cblTemp.Items[i].Value);
                    if (intSelect == intID)
                    {
                        cblTemp.Items[i].Selected = true;
                        break;
                    }
                }
            }	            
        }
        //Select du lieu cua CheckBoxList theo DataTable
        public static void selectCheckBoxList(CheckBoxList cblTemp, DataTable dtTemp, string strID)
        {
            for (int j = 0; j < dtTemp.Rows.Count; j++)
            {
                int intID = Convert.ToInt32(dtTemp.Rows[j][strID]);
                for (int i = 0; i < cblTemp.Items.Count; i++)
                {
                    int intSelect = Convert.ToInt32(cblTemp.Items[i].Value);
                    if (intSelect == intID)
                    {
                        cblTemp.Items[i].Selected = true;
                        break;
                    }
                }
            }
        }
        //Select du lieu cua CheckBoxList theo DataRow[]
        public static void selectCheckBoxList(CheckBoxList cblTemp, DataRow[] drTemp, string strID)
        {
            for (int j = 0; j < drTemp.Length; j++)
            {
                int intID = Convert.ToInt32(drTemp[j][strID]);
                for (int i = 0; i < cblTemp.Items.Count; i++)
                {
                    int intSelect = Convert.ToInt32(cblTemp.Items[i].Value);
                    if (intSelect == intID)
                    {
                        cblTemp.Items[i].Selected = true;
                        break;
                    }
                }
            }
        }       
        //Select du lieu cua RadioButtonList
        public static void selectRadioButtonList(RadioButtonList rblTemp, string strValue)
        {
            for (int i = 0; i < rblTemp.Items.Count; i++)
            {
                if (rblTemp.Items[i].Value.Trim() == strValue.Trim())
                {
                    rblTemp.Items[i].Selected = true;
                    break;
                }
            }
        }        
#endregion

#region " Get Control"
        public static string getTextBox(TextBox txtemp)
        {
            return txtemp.Text;
        }
        public static string getCheckBoxList(CheckBoxList cblTemp)
        {
            string strIDs = ",";
            for (int i = 0; i < cblTemp.Items.Count; i++)
            {
                if (cblTemp.Items[i].Selected)
                {
                    strIDs += cblTemp.Items[i].Value + ",";
                }
            }
            return strIDs;
        }
        public static string getCheckBoxListRole(CheckBoxList cblTemp)
        {
            string strIDs = "";
            for (int i = 0; i < cblTemp.Items.Count; i++)
            {
                if (cblTemp.Items[i].Selected)
                {
                    strIDs += cblTemp.Items[i].Value + ",";
                }
            }
            return strIDs;
        }
        public static string getListBox(ListBox ltbTemp, bool checkValues)
        {
            string strIDs = ",";
            string strNames = "";
            if (checkValues)
            {
                for (int i = 0; i < ltbTemp.Items.Count; i++)
                {
                    if (ltbTemp.Items[i].Selected)
                    {
                        strIDs += ltbTemp.Items[i].Value + ",";
                    }
                }
                return strIDs;
            }
            else
            {
                for (int i = 0; i < ltbTemp.Items.Count; i++)
                {
                    if (ltbTemp.Items[i].Selected)
                    {
                        if (i == (ltbTemp.Items.Count - 1))
                        {
                            strNames += ltbTemp.Items[i].Text;
                        }
                        else
                        {
                            strNames += ltbTemp.Items[i].Text+ ",";
                        }
                    }
                }
                return strNames;
            }
            
        }
#endregion
    }
}
